1 using System;
2 using UnityEngine;
3 using UnityEngine.EventSystems;
4 using UnityEngine.UI;
5
6 namespace UnityStandardAssets.CrossPlatformInput
7 {
8 [RequireComponent(typeof(Image))]
9 public class TouchPad : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
10 {
11 // Options for which axes to use
12 public enum AxisOption
13 {
14 Both, // Use both
15 OnlyHorizontal, // Only horizontal
16 OnlyVertical // Only vertical
17 }
18
19
20 public enum ControlStyle
21 {
22 Absolute, // operates from teh center of the image
23 Relative, // operates from the center of the initial touch
24 Swipe, // swipe to touch touch no maintained center
25 }
26
27
28 public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
29 public ControlStyle controlStyle = ControlStyle.Absolute; // control style to use
30 public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
31 public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
32 public float Xsensitivity = 1f;
33 public float Ysensitivity = 1f;
34
35 Vector3 m_StartPos;
36 Vector2 m_PreviousDelta;
37 Vector3 m_JoytickOutput;
38 bool m_UseX; // Toggle for using the x axis
39 bool m_UseY; // Toggle for using the Y axis
40 CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
41 CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
42 bool m_Dragging;
43 int m_Id = -1;
44 Vector2 m_PreviousTouchPos; // swipe style control touch
45
46
47 #if !UNITY_EDITOR
48 private Vector3 m_Center;
49 private Image m_Image;
50 #else
51 Vector3 m_PreviousMouse;
52 #endif
53
54 void OnEnable()
55 {
56 CreateVirtualAxes();
57 }
58
59 void Start()
60 {
61 #if !UNITY_EDITOR
62 m_Image = GetComponent<Image>();
63 m_Center = m_Image.transform.position;
64 #endif
65 }
66
67 void CreateVirtualAxes()
68 {
69 // set axes to use
70 m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
71 m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
72
73 // create new axes based on axes to use
74 if (m_UseX)
75 {
76 m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
77 CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
78 }
79 if (m_UseY)
80 {
81 m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
82 CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
83 }
84 }
85
86 void UpdateVirtualAxes(Vector3 value)
87 {
88 value = value.normalized;
89 if (m_UseX)
90 {
91 m_HorizontalVirtualAxis.Update(value.x);
92 }
93
94 if (m_UseY)
95 {
96 m_VerticalVirtualAxis.Update(value.y);
97 }
98 }
99
100
101 public void OnPointerDown(PointerEventData data)
102 {
103 m_Dragging = true;
104 m_Id = data.pointerId;
105 #if !UNITY_EDITOR
106 if (controlStyle != ControlStyle.Absolute )
107 m_Center = data.position;
108 #endif
109 }
110
111 void Update()
112 {
113 if (!m_Dragging)
114 {
115 return;
116 }
117 if (Input.touchCount >= m_Id + 1 && m_Id != -1)
118 {
119 #if !UNITY_EDITOR
120
121 if (controlStyle == ControlStyle.Swipe)
122 {
123 m_Center = m_PreviousTouchPos;
124 m_PreviousTouchPos = Input.touches[m_Id].position;
125 }
126 Vector2 pointerDelta = new Vector2(Input.touches[m_Id].position.x - m_Center.x , Input.touches[m_Id].position.y - m_Center.y).normalized;
127 pointerDelta.x *= Xsensitivity;
128 pointerDelta.y *= Ysensitivity;
129 #else
130 Vector2 pointerDelta;
131 pointerDelta.x = Input.mousePosition.x - m_PreviousMouse.x;
132 pointerDelta.y = Input.mousePosition.y - m_PreviousMouse.y;
133 m_PreviousMouse = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f);
134 #endif
135 UpdateVirtualAxes(new Vector3(pointerDelta.x, pointerDelta.y, 0));
136 }
137 }
138
139
140 public void OnPointerUp(PointerEventData data)
141 {
142 m_Dragging = false;
143 m_Id = -1;
144 UpdateVirtualAxes(Vector3.zero);
145 }
146
147 void OnDisable()
148 {
149 if (CrossPlatformInputManager.AxisExists(horizontalAxisName))
150 CrossPlatformInputManager.UnRegisterVirtualAxis(horizontalAxisName);
151
152 if (CrossPlatformInputManager.AxisExists(verticalAxisName))
153 CrossPlatformInputManager.UnRegisterVirtualAxis(verticalAxisName);
154 }
155 }
156 }